#######################################################
#A COMMON-SPACE TECHNIQUE FOR VOTERS AND LEGISLATORS
#WITH APPLICATION TO THE 2010/2011 Elections
####################################################### 

##NB - Load the Master file first
library(doParallel)

##We extract Senators 1&2 and identify unique governors
gov <- paste(CurrentGovName,"-",inputstate)

gov_unique <- unique(gov)

##create a new cces variable for house scaling
cces$governor_scale <- NULL


##Now, we go through the data and make a unique column for each governor

#temp_tot is a matrix that will hold all of the data
temp_tot <- NULL

for (i in 1:length(gov_unique)){
  
  temp <- ifelse(gov_unique[i]==gov, CC334B, 999)
  
  temp_tot <- cbind(temp_tot, temp)
  
  if(i==1){
    colnames(temp_tot) <- c(sub(" ", " ", gov_unique)[i])
  }
  
  if(i>1){
    colnames(temp_tot) <- c(colnames(temp_tot)[1:(ncol(temp_tot)-1)], 
                            sub(" ", " ", gov_unique)[i])
  }
}

################################################
#To the matrix of senators, we add individuals,
#parties, and the president
################################################

self <- CC334A
obama <- CC334C
democrats <- CC334E
republicans <- CC334F
scotus <- CC334P
teaparty <- CC334G
romney <- CC334D

placement_matrix <- cbind(self, obama, democrats, republicans, temp_tot)

for (j in 1:ncol(placement_matrix)){
  placement_matrix[,j] <- as.numeric(placement_matrix[,j])
  placement_matrix[,j] <- replace(placement_matrix[,j], placement_matrix[,j]==8 | is.na(placement_matrix[,j]), 999)
}  

###################################################
#Next, we estimate AM step-by-step for each state
###################################################

##We need one overall estimate across all respondents
overall_scale <- aldmck(placement_matrix[,1:4], polarity=2, respondent=1, missing=999, verbose=TRUE)

#save the overall dem and rep pos
dem_overall <- overall_scale$stim[2]
rep_overall <- overall_scale$stim[3]

states_unique <- unique(inputstate)

##here are the voters' preferences
ideal_points <- overall_scale$resp$ideal


##now governors
placement_holder <- NULL
med_voter <- NULL

for (i in 1:length(states_unique)){
  
  if (states_unique[i]!="District of Columbia"){
  
    #subset by state
    temp <- subset(placement_matrix, inputstate==states_unique[i])  
    
    #remove stimuli that are not measured
    drop <- NULL
    for (j in 1:ncol(temp)) if(sum(temp[,j]==999, na.rm=TRUE)==nrow(temp)) drop <- c(drop, j)
    temp <- temp[,-drop]
    
    scaling <- aldmck(temp, polarity=2, respondent=1, missing=999, verbose=FALSE)
    
    registerDoParallel(cores=8)
    scaling_boot <- foreach(i=1:200, .combine='cbind') %dopar% 
      aldmck(temp[sample(1:nrow(temp),nrow(temp),replace=TRUE),], polarity=2, respondent=1, 
             missing=999)$stim
    
    #recale from state space back to overall space
    #using the equ OVERALL = slope*STATE - slope*STATE_DEM + OVERALL_DEM
    
    slope <- (overall_scale$stim[3]-overall_scale$stim[2])/(scaling$stim[3]-scaling$stim[2])
    rescaling <- slope*scaling$stim - slope*scaling$stim[2] + overall_scale$stim[2]
    
    rescaling_boot <- slope*scaling_boot[4,] - slope*scaling$stim[2] + overall_scale$stim[2]
    
    #add results to cces dataset...
    cces$governor_scale[inputstate==states_unique[i]] <- rescaling[4]
    
    #save results for elites
    tmp <- c(rescaling[4], quantile(rescaling_boot,c(0.05,0.95)))
    tmp <- data.frame(names(rescaling)[4],t(tmp))
    colnames(tmp) <- c("governor","idealpt","lower","upper")
    placement_holder <- rbind(placement_holder, tmp)
    
    #and for voters
    voter_boot <- foreach(i=1:200, .combine='cbind') %dopar% 
      aldmck(temp[sample(1:nrow(temp),nrow(temp),replace=TRUE),], polarity=2, respondent=1, 
             missing=999)$resp$idealpt
    m_boot <- apply(voter_boot,2,mean,na.rm=TRUE)
    m_boot <- slope*m_boot - slope*scaling$stim[2] + overall_scale$stim[2]
    
    tmp2 <- c(mean(m_boot), quantile(m_boot,c(0.05,0.95)))
    tmp2 <- data.frame(states_unique[i],t(tmp2))
    colnames(tmp2) <- c("state","mean_voter","m_lower","m_upper")
    med_voter <- rbind(med_voter, tmp2)
    
    print(tmp)
  }
}

library(ggplot)

##Plot the results
placement_holder <- data.frame(placement_holder, med_voter)

placement_holder$governor <- unlist(strsplit(as.character(placement_holder$governor)," - "))[seq(1,2*nrow(placement_holder),by=2)]

placement_holder$governor <- factor(placement_holder$governor)
placement_holder$governor <- reorder(placement_holder$governor, placement_holder$idealpt)

ggplot(placement_holder, aes(x = idealpt, xmin = lower, xmax = upper, y = governor )) +
  geom_point() + geom_segment( aes(x = lower, xend = upper,
                                   y = governor, yend=governor)) +
  geom_text(aes(x=mean_voter,y=governor,label=state,colour="red",size=.7)) + 
#    geom_segment(aes(x = m_lower, xend = m_upper, y = governor, yend=governor, colour="red")) +
  theme_bw() + theme(axis.title.x = element_text(size = 12, vjust = .25), legend.position="none") +
  xlab("Ideal Points (State Mean in Red, Legislators in Black)") + ylab("") 


